package com.cndw.kungfu.model.main;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.core.cache.CacheLottery;
import com.cndw.kungfu.core.cache.CacheScript;
import com.cndw.kungfu.core.cache.CacheVocation;
import com.cndw.kungfu.dao.IBusinessDao;
import com.cndw.kungfu.domain.LotteryMode;
import com.cndw.kungfu.domain.Notice;
import com.cndw.kungfu.domain.Script;
import com.cndw.kungfu.domain.Vocation;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.SimpleModuleManager;

@Component
public class CongfigServer {	
	@Autowired
	private SimpleModuleManager simpleModuleManager;
	@Autowired
	private CacheVocation cacheVocation;
	@Autowired
	private CacheScript cacheScript;

	@Autowired
	private CacheLottery cacheLottery;

	
	/**
	 * 获取系统config表数据
	 * @return
	 */
	public Map<String, String> loadConfigFromDb(){
		return simpleModuleManager.loadConfigFromDb();
	}

	
	
	public Vocation getVocation(int vocationId){
		return cacheVocation.getOne(vocationId);		 
	}
	
	public Collection<Vocation> getVocations(){
		return cacheVocation.getAll();
	}
	public  String[] getVocationNames(){
		Collection<Vocation> vocations = getVocations();
		String[] sb = new String[vocations.size()+1];
		for (Vocation e : vocations) {
			sb[e.getId()] = e.getName();			
		}return sb;			
	}
	
	public Script getScript(int scriptId){
		return cacheScript.getOne(scriptId);
	}
	
	
	
	public Collection<Script> getScripts(){
		return cacheScript.getAll();
	}

	
	public List<Notice> getBusinessNoticesDB(){		
		return simpleModuleManager.loadBusinessNoticesFromDb();		
	}
	
	@SuppressWarnings("unchecked")
	public List<Notice> getBusinessNoticesOld(){
		
		ConcurrentHashMap<String, Object> map = Context.getDataServer().getBusinessAllMap();
		if (map.containsKey(IBusinessDao.B_NOTICE_TABLE)) {
			return (List<Notice>) map.get(IBusinessDao.B_NOTICE_TABLE);
		}else {
			List<Notice> list = simpleModuleManager.loadBusinessNoticesFromDb();
			if (list != null) {
				map.put(IBusinessDao.B_NOTICE_TABLE,list);
			}
			return list;
		}
		
	}
	/**
	 * 获取某个类别下的抽奖
	 * @param type
	 * @return
	 */
	public Collection<LotteryMode> getLotteryModes(int type){
		return cacheLottery.getLotteryModes(type);
	}

	
	
	
}
